﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;

namespace MessengerBot
{
    /// <summary>
    /// This <see cref="ILogger"/> implementation wraps up multiple
    /// <see cref="ILogger"/> instances, so that a single log object
    /// can be given to the <see cref="Bot"/> instance, yet multiple
    /// log destinations can be used.
    /// </summary>
    public sealed class MultiLogger : ILogger
    {
        /// <summary>
        /// This is the backing field for the <see cref="Loggers"/> property.
        /// </summary>
        private readonly List<ILogger> _Loggers = new List<ILogger>();

        /// <summary>
        /// Gets the collection of <see cref="ILogger"/> instances registered with this
        /// <see cref="MultiLogger"/>.
        /// </summary>
        public Collection<ILogger> Loggers
        {
            get
            {
                return new Collection<ILogger>(_Loggers);
            }
        }

        #region ILogger Members

        /// <summary>
        /// Logs a single message to the log destination.
        /// </summary>
        /// <param name="level">The level of the message, used for filtering.</param>
        /// <param name="message">The message to log.</param>
        public void Log(LogLevel level, string message)
        {
            foreach (ILogger logger in _Loggers)
                if (logger != null)
                    logger.Log(level, message);
        }

        #endregion
    }
}